package com.tjn.string_;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class StringBuilder_ {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello");
        /*
        public final class StringBuilder
        extends AbstractStringBuilder
        implements java.io.Serializable, CharSequence
         */
        //1.StringBuilder的父类是AbstractStringBuilder，和StringBuffer一样
        //2.StringBuilder实现了Serializable接口，和StringBuffer一样,即他的对象可以串行化
        //3.父类AbstractStringBuilder 有属性 char[] value,不是 final，是可变的字符串
        //4.StringBuilder是final类，不能被继承
        //5.StringBuilder效率比String高，但是线程不安全

        //StringBuilder和StringBuffer很类似，都是可变的字符串，方法一样
        //效率 StringBuilder > StringBuffer > String,测试：

        long startTime = 0L;
        long endTime = 0L;
        StringBuffer buffer = new StringBuffer("");
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 80000; i++) {
            buffer.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer 的执行时间：" + (endTime - startTime));

        StringBuilder builder = new StringBuilder("");
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 80000; i++) {
            builder.append(String.valueOf(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuilder 的执行时间：" + (endTime - startTime));

        String text = "";
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 80000; i++) {
            text = text + i;
        }
        endTime = System.currentTimeMillis();
        System.out.println("String 的执行时间：" + (endTime - startTime));
    }
}
